package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author ZJ
 */
@CanalEventListener///申明当前是canal的监听类
public class BusinessListener {

		@Autowired
		RabbitTemplate rabbitTemplate;

        @ListenPoint(schema = "changgou_business",table = "tb_ad")//表示监听的数据库 和监听的表
        public void adUpdate(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
           /* //获取修改前的数据
            List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
            for (CanalEntry.Column column : beforeColumnsList) {
                System.out.println("改变之前的数据："+column.getName()+"::"+column.getValue());
            }

            //获取改变后的数据
            List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
            for (CanalEntry.Column column : afterColumnsList) {
                System.out.println("改变之后的数据："+column.getName()+"::"+column.getValue());
            }*/

           ///获取最新的消息，打印并发送到自定义的MQ队列“ad_update_queue”中
			List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
			for (CanalEntry.Column column : beforeColumnsList) {
				if ("position".equals(column.getName())){
					//找到position对应的value
					String value = column.getValue();
					//打印
					System.out.println(value);
					//发送给MQ队列，在此之前，先注入MQ模板
					rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATE_QUEUE,value);
				}
			}
		}

}
